package com.company.笔试.数组中重复的数;

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param numbers int整型一维数组 
     * @return int整型
     */
    public int duplicate (int[] numbers) {
        // write code here
        TreeMap<Integer,Integer> map = new TreeMap<>();
        
        for(int i : numbers){
            if(map.containsKey(i)){
                System.out.println(i +" "+ (map.get(i)+1));
                map.put(i,map.get(i)+1);
            }else {

                map.put(i,1);
            }
        }
        System.out.println(map.toString());
        ArrayList<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        System.out.println(list);
        int res = list.get(0).getKey();
        return res;
    }


    public int duplicateNew (int[] numbers) {
        if (numbers.length==0) return -1;
        if (numbers.length==1) return  numbers[0];
        HashSet<Integer> set = new HashSet<>();
        for (int number : numbers) {
            if (set.contains(number)) return number;
            set.add(number);


        }
        return -1;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.duplicate(new int[]{2,1,3,1,4}));
    }
}